/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.alpha.parse;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.htmlparser.Node;
import org.htmlparser.Parser;
import org.htmlparser.filters.TagNameFilter;
import org.htmlparser.tags.Bullet;
import org.htmlparser.tags.BulletList;
import org.htmlparser.tags.CompositeTag;
import org.htmlparser.tags.LinkTag;
import org.htmlparser.util.NodeList;
import org.htmlparser.util.ParserException;
import org.htmlparser.util.SimpleNodeIterator;

/**
 *
 * @author alvar
 */
public class D6ForumParser implements ForumParser{

    protected String url;
    protected String forumName;
    protected List<String> categoryList;
    protected Map<Integer, Map<Integer, String>> blockList;
    protected Map<Integer, Map<Integer, String>> subBlockList;

    public D6ForumParser(String url) {
        this.url = url;
        try {
            init();
        } catch (ParserException ex) {
            Logger.getLogger(D6ForumParser.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public List getCategoryList() {
        return categoryList;
    }

    public Map getBlockList() {
        return blockList;
    }
    
    public Map getSubBlockList(){
        return subBlockList;
    }
    
    public String getForumName(){
        return forumName;
    }

    private void init() throws ParserException {
        categoryList = new ArrayList<String>();
        blockList = new LinkedHashMap<Integer, Map<Integer, String>>();
        subBlockList = new LinkedHashMap<Integer, Map<Integer, String>>();
        Parser p;
        p = new Parser(url);
        forumName = new Parser(url).parse(new TagNameFilter("title")).asString();
        NodeList list = p.parse(new TagNameFilter("li"));
        for (Node n : list.toNodeArray()) {
            //most outter, iterator different category
            if (!(n.getChildren().extractAllNodesThatMatch(new TagNameFilter("h3")).toHtml().trim().equals(""))) {
                //filte out the right category tag
                //parse out categorylist
                categoryList.add(n.getChildren().elementAt(0).toPlainTextString());
                Integer cid = n.getChildren().elementAt(0).toPlainTextString().hashCode();
                //parse out block of each category
                SimpleNodeIterator i = null;
                if (n.getChildren().elementAt(1) instanceof CompositeTag) {
                    i = ((CompositeTag) n.getChildren().elementAt(1)).elements();
                    LinkedHashMap block = new LinkedHashMap<Integer, String>();
                    LinkedHashMap subBlock = null;
                    blockList.put(cid, block);
//                    LinkedHashMap subBlock = new LinkedHashMap<Integer,String>();
//                    subBlockList.put(cid, subBlock);
                    //iterate each block in each category
                    int fid = 0;
                    while (i.hasMoreNodes()) {
                        Node h = i.nextNode();
                        if (h instanceof Bullet) {
                            for (Node k : h.getChildren().toNodeArray()) {
                                
                                //filter out the hyperlink
                                if (k instanceof LinkTag) {
                                    fid = Integer.parseInt(((LinkTag) k).getLink().substring(((LinkTag) k).getLink().indexOf("fid-") + 4, ((LinkTag) k).getLink().indexOf(".html")));
//                                    System.out.println(k.toHtml() + "->link");
                                    subBlock = new LinkedHashMap<Integer, String>();
                                    subBlockList.put(fid, subBlock);
                                    block.put(fid, k.toPlainTextString());
                                }
                                //filter out the subblock
                                if (k instanceof BulletList) {
//                                    System.out.println(k.toHtml() + "-> blist");
//                                    System.out.println(k.getChildren().toHtml());
                                    SimpleNodeIterator s = ((CompositeTag) k).elements();
                                    while (s.hasMoreNodes()) {
                                        Node sublocknode = s.nextNode();
                                        if (sublocknode instanceof Bullet && sublocknode.getFirstChild() instanceof LinkTag) {
                                            int subFid = Integer.parseInt(((LinkTag) sublocknode.getFirstChild()).getLink().substring(((LinkTag) sublocknode.getFirstChild()).getLink().indexOf("fid-") + 4, ((LinkTag) sublocknode.getFirstChild()).getLink().indexOf(".html")));
                                            subBlock.put(subFid, sublocknode.getFirstChild().toPlainTextString());
                                        }

                                    }

                                }


                            }
                        }


                    }
                }


            }
        }


    }
}
